const gameWidth = gameDom.clientWidth;
class Pipe extends Rectangle {
    constructor(height, top, speed, dom) {
        super(52, height, top, gameWidth, 0, speed, dom);
    }

    onMove() {
        if (this.left < -this.width) {
            //移除
            this.dom.remove();
        }
    }
}
function getRandom(max, min) {
    return Math.floor(Math.random() * (max - min) + min);
}
class PipePare {
    constructor(speed) {
        this.spaceHeight = 150;  //空隙的高度
        this.minHeight = 80;//水管最小的高度
        this.maxHeight = landTop - this.minHeight - this.spaceHeight; //水管最大高度

        const upDom = document.createElement("div");
        const downDom = document.createElement("div");
        upDom.className = "pipe up";
        downDom.className = "pipe down";


        const upHeight = getRandom(this.minHeight, this.maxHeight);
        this.upPipe = new Pipe(upHeight, 0, speed, upDom) //上水管

        const downHeight = landTop - upHeight - this.spaceHeight;
        const downPipeTop = landTop - downHeight;
        this.downPipe = new Pipe(downHeight, downPipeTop, speed, downDom)  //下水管

        gameDom.appendChild(upDom);
        gameDom.appendChild(downDom);
    }

    get useLess() {
        return this.upPipe.left < -this.upPipe.width;
    }

    move(duration) {
        this.upPipe.move(duration);
        this.downPipe.move(duration);
    }
}
class PipePareProducer {
    constructor(speed) {
        this.speed = speed;
        this.pairs = [];
        this.timer = null;
        this.tick = 2000;
    }
    startProduce() {
        if (this.timer) {
            return;
        }
        this.timer = setInterval(() => {
            this.pairs.push(new PipePare(this.speed));
            //移除掉用不到的柱子
            for (let i = 0; i < this.pairs.length; i++) {
                let pair = this.pairs[i];
                if (pair.useLess) {
                    //没用的柱子对
                    this.pairs.splice(i, 1);
                    i--;
                }
            }
        }, this.tick);
    }
    stopProduce() {
        clearInterval(this.timer);
        this.timer = null;
    }

}
